package com.ning.mvvmplayandroid.ui.activity

import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.google.android.material.tabs.TabLayoutMediator.TabConfigurationStrategy
import com.ning.mvvmplayandroid.R
import com.ning.mvvmplayandroid.base.BaseActivity
import com.ning.mvvmplayandroid.ui.fragment.HomeFragment
import com.ning.mvvmplayandroid.ui.fragment.KnowledgeFragment
import com.ning.mvvmplayandroid.ui.fragment.NavigationFragment
import com.ning.mvvmplayandroid.ui.fragment.ProjectFragment
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar.*
import java.util.*

/**
 * @作者: njb
 * @时间: 2020/1/13 12:51
 * @描述: 主界面
 */
open class MainActivity : BaseActivity() {
    private val fragmentList: MutableList<Fragment> =
        ArrayList()
    private val strings =
        arrayOf("首页", "知识", "导航", "项目")

    override val layoutId: Int
        get() = R.layout.activity_main

    override fun initView() {
        fragmentList.add(HomeFragment())
        fragmentList.add(KnowledgeFragment())
        fragmentList.add(NavigationFragment())
        fragmentList.add(ProjectFragment())
        viewPager!!.adapter = object : FragmentStateAdapter(this) {
            override fun createFragment(position: Int): Fragment {
                return fragmentList[position]
            }

            override fun getItemCount(): Int {
                return fragmentList.size
            }
        }
        viewPager.offscreenPageLimit = 3
        val tabLayoutMediator = TabLayoutMediator(
            tab_layout,
            viewPager,
            TabConfigurationStrategy { tab: TabLayout.Tab, position: Int ->
                tab.text = strings[position]
            }
        )
        tabLayoutMediator.attach()
        initToolBar()
    }

    private fun initToolBar() {
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(false)
        supportActionBar!!.setHomeButtonEnabled(false)
        iv_search.setImageResource(R.drawable.ic_baseline_search_24)
    }

    override fun addListener() {
        iv_search.setOnClickListener(View.OnClickListener {
            startActivity(
                Intent(
                    this@MainActivity,
                    SearchActivity::class.java
                )
            )
        })
    }
}